var base_url = getApp().globalData.base_url
var app = getApp()
Page({
    data: {
        oldBudgetAmount: '',
        newBudgetAmount: '',
    },

    onConfirm() {
        let userId = app.globalData.userId
        let budgetAmount = this.data.newBudgetAmount

        // 校验输入金额是否合法
        if (isNaN(budgetAmount) || !(budgetAmount % 1 === 0) || budgetAmount < 0) {
            // TODO 提醒用户输入金额错误
            return
        }
        wx.request({
            url: base_url + '/users/budget',
            method: 'PUT',
            header: {
              "content-type": 'application/x-www-form-urlencoded'
            },
            data: {
              userId: userId,
              budgetAmount: budgetAmount
            },
            success(res) {
              if (res.data.resultCode == 200) {
                  console.log(res.data.message)
                  app.globalData.budgetAmount = budgetAmount
                  // TODO 弹出修改成功提示，跳转回主页面
                  wx.navigateBack({
                    delta: 0,
                  })
              } else {
                // TODO 提示修改预算失败
              }
            }
          })
    },

    onCurrentInput(e) {
        this.setData({
            newBudgetAmount: Number(e.detail.value)
        })
    },

    onLoad() {
        this.setData({
            oldBudgetAmount: app.globalData.budgetAmount,
        })
    }
});
